<?php

namespace app\logic\push;

use app\logic\Page;
use app\model\MessageSystem AS ModelMessageSystem;
use app\model\MessageClient AS ModelMessageClient;
use app\model\MessageContent AS ModelMessageContent;

class ClientPush extends Push
{
    private $str_model_name;

    public function __construct($str_model_name)
    {
        parent::__construct();
        if (!in_array($str_model_name, $this->arr_client_key)) return false;
        $this->str_model_name = $str_model_name;
    }

    /**
     * 数据透传
     * @param string $str_map_id 用户编号
     * @param string $str_desc 推送标题(描述)
     * @param string $json_params 推送的数据
     * @return bool
     */
    public function dataPassThrough($str_map_id, $str_desc, $json_params)
    {
        return $this->pushMessageToUser($this->str_model_name, $str_map_id, 'all', 1, '', $str_desc, $json_params, 0, 0, '0000-00-00 00:00:00', 0, 0);
    }

    /**
     * 获取推送系统信息列表
     */
    public function getSystemMessageList($int_now_page, $int_list_rows)
    {
        $str_where = '(FIND_IN_SET("' . $this->str_model_name . '",client) AND release_status=1) OR (FIND_IN_SET("' . $this->str_model_name . '",client) AND release_status=2 AND subscribe_time<"' . date('Y-m-d H:i:s') . '")';
        $str_field = 'id AS messageId,title,desc,params,gmt_create AS gmtCreate';
        $arr_info = Page::getInstance()->getList(ModelMessageSystem::getInstance(), $str_where, $str_field, $int_now_page, $int_list_rows, 'id DESC', $str_key = 'message');
        _returnData($arr_info);
    }

    /**
     * 获取推送活动信息列表
     */
    public function getActivityMessageList($int_id, $int_now_page, $int_list_rows)
    {

        $str_where = '(map_id=' . $int_id . ' AND client="' . $this->str_model_name . '" AND is_show=1) OR (map_id=' . $int_id . ' AND client="' . $this->str_model_name . '" AND is_show=1 AND subscribe_time<"' . date('Y-m-d H:i:s') . '")';

        $str_field = 'id AS messageId,title,desc,params,gmt_create AS gmtCreate';
        $arr_info = Page::getInstance()->getList(ModelMessageClient::getInstance(), $str_where, $str_field, $int_now_page, $int_list_rows, 'id DESC', 'messageList');
        _returnData($arr_info);
    }

    /**
     * 获取消息详情
     * @param $str_type
     * @param $int_message_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMessageInfo($str_type, $int_message_id)
    {
        _toValidate('id', $int_message_id, 10114); // 消息编号不正确

        _toCompare(in_array($str_type, ['system', 'activity']), '===', false, 10115); // 消息内容不存在
        $arr_info = (array)ModelMessageContent::getInstance()->toFind(['id' => $int_message_id], 'title,content');
        _toCompare(isset($arr_info['content']), '===', false, 10115); // 消息内容不存在

        _returnData(['details' => $arr_info]);
    }

    /**
     * 更新为已读消息
     * @param $int_message_id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setActivityMessageIsRead($int_id, $int_message_id)
    {
        if (!($int_message_id > 0)) _returnData();
        ModelMessageClient::getInstance()->toUpdate(['id' => $int_message_id, 'map_id' => $int_id, 'client' => $this->str_model_name, 'is_read' => 0], ['is_read' => 1]);
        _returnData();
    }

    /**
     * 消息全部为已读
     * @param $int_message_id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setActivityMessageAllIsRead($int_id)
    {
        ModelMessageClient::getInstance()->toUpdate(['map_id' => $int_id, 'client' => $this->str_model_name, 'is_read' => 0], ['is_read' => 1]);
        _returnData();
    }
}
